<?php
//require_once('../user/wxbrower.php');
require_once("../includes/public.inc.php");
//使用防伪码进行查询，如果数据库有这个码，然后放到随机库里面查询，如果有就中奖没有就写很遗憾没有中奖randomcode
$fwm = mysql_fetch_array(mysql_query("select id from ".DBQIAN."fwm where fwm='".trim($_POST['fwm'])."' order by id desc limit 1"));
//print_r($fwm);exit;
if ($fwm)
{
	$ipc=get_ip_place();
//	print_r($ipc);exit;
	//查询是否中奖
	$praisecode = mysql_fetch_array(mysql_query("select id from ".DBQIAN."randomcode where randcode='".$fwm['id']."' and status=0 order by id desc limit 1"));
	if ($praisecode)//中奖
	{
		//添加用户信息
		$result = mysql_query("insert into ".DBQIAN."user(fwm,name,tel,province,city,address,car_type,ip,ip_address,praise,status,time) values('".$_POST['fwm']."','".$_POST['name']."','".$_POST['tel']."','".$_POST['province']."','".$_POST['city']."','".$_POST['address']."','".$_POST['car_type']."','".$ipc['cip']."','".$ipc['cname']."','".'中奖'."',1,'".date('Y-m-d H:i:s',time())."')");
		//$fr = mysql_query("update ".DBQIAN."fwm set time='".date('Y-m-s H:i:s',time())."',status=1");
		//print_r($_POST);
		if ($result)
		{
			header("Location: fbkresult.php?s=".$_POST['fwm']."&id=".$fwm['id']."&status=1");
			exit;
		}
		else 
		{
			//输出，提交错误请联系管理员
			$url = "fbk.php?error=404";
			echo "<script language='javascript' type='text/javascript'>";
			echo "alert('提交数据错误，请联系管理员！');window.location.href='$url'";
			echo "</script>";
			exit;
		}
	}
	else //不中奖
	{
		//添加用户信息
		$result = mysql_query("insert into ".DBQIAN."user(fwm,name,tel,province,city,address,car_type,ip,ip_address,praise,status,time) values('".$_POST['fwm']."','".$_POST['name']."','".$_POST['tel']."','".$_POST['province']."','".$_POST['city']."','".$_POST['address']."','".$_POST['car_type']."','".$ipc['cip']."','".$ipc['cname']."','".'不中奖'."',0,'".date('Y-m-d H:i:s',time())."')");
		//$fr = mysql_query("update ".DBQIAN."fwm set time='".date('Y-m-s H:i:s',time())."',status=1");
		//print_r($_POST);
		if ($result)
		{
			header("Location: fbkresult.php?s=".$_POST['fwm']."&id=".$fwm['id']."&status=0");
			exit;
		}
		else 
		{
			//输出，提交错误请联系管理员
			$url = "fbk.php?error=404";
			echo "<script language='javascript' type='text/javascript'>";
			echo "alert('提交数据错误，请联系管理员！');window.location.href='$url'";
			echo "</script>";
			exit;
		}	
	}
}
else 
{
//	header("Location: fbk.php");
	$url = "fbk.php?error=404";
	echo "<script language='javascript' type='text/javascript'>";
	echo "alert('该防伪码为非法防伪码！');window.location.href='$url'";
	echo "</script>";
	exit;
}
exit; 
function get_ip_place()
{
	$ipc=file_get_contents("http://pv.sohu.com/cityjson?ie=utf-8");
	$substr = substr($ipc,18);
	$json = str_replace('};','}',$substr);
	$b = json_decode($json,true);
	return $b;
}
